package com.yantu.controller.student;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yantu.bean.ExamPaperAnswer;
import com.yantu.bean.Subject;
import com.yantu.bean.User;
import com.yantu.mapper.ExamPaperAnswerMapper;
import com.yantu.mapper.SubjectMapper;
import com.yantu.mapper.UserMapper;
import com.yantu.util.Result;
import com.yantu.view.RecordPageView;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@ApiModel("学生考试记录分页接口")
@RequestMapping("/student")
public class StudentExampaperRecordPageController {

    @Autowired
    ExamPaperAnswerMapper examPaperAnswerMapper;
    @Autowired
    SubjectMapper subjectMapper;
    @Autowired
    UserMapper userMapper;

    //试卷分页
    @GetMapping("/RecordPage")
    @ApiOperation("考试记录分页")
    public Result recordPage(@ModelAttribute() RecordPageView recordPageView) {
        PageHelper.startPage(recordPageView.getPageIndex(), recordPageView.getPageSize());

        List<ExamPaperAnswer> examPaperAnswers = examPaperAnswerMapper.selectList(null);

        if(examPaperAnswers!=null){
            PageInfo pageInfo = new PageInfo(examPaperAnswers);
            List<ExamPaperAnswer> examPaperAnswers1 = pageInfo.getList();
            Map<String, Object> map = new HashMap<>();
            map.put("total", pageInfo.getTotal());
            List<Object> list1 = new ArrayList();
            for (ExamPaperAnswer examPaperAnswer : examPaperAnswers1) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("id",examPaperAnswer.getId());
                map1.put("createTime",examPaperAnswer.getCreateTime());
                map1.put("userScore",examPaperAnswer.getUserScore());


                Subject subject = subjectMapper.selectById(examPaperAnswer.getSubjectId());
                map1.put("subjectName", subject.getName());
                map1.put("subjectId", examPaperAnswer.getSubjectId());
                map1.put("questionCount", examPaperAnswer.getQuestionCount());
                map1.put("questionCorrect", examPaperAnswer.getQuestionCorrect());
                map1.put("paperScore", examPaperAnswer.getPaperScore());
                map1.put("doTime",examPaperAnswer.getDoTime());
                map1.put("paperType",examPaperAnswer.getPaperType());
                map1.put("systemScore",examPaperAnswer.getSystemScore());
                map1.put("status",examPaperAnswer.getStatus());
                map1.put("paperName",examPaperAnswer.getPaperName());


                User user = userMapper.selectById(examPaperAnswer.getCreateUser());

                map1.put("userName",user.getUserName());

                list1.add(map1);
            }
            map.put("list", list1);
            return Result.succ(200, "查询成功", map);

        }
        else return Result.succ(200, "查询不到数据", null);


    }
}
